﻿Imports Global_Visas_Business_Layer
Imports System.Text
Imports System.Data
Imports System.ComponentModel

Partial Public Class frmAddPayment

    Private _PaymentTypeGroup As Integer
    Private _PaymentMode As Integer
    Private m_strCurrencyChar As String

    Public Property PaymentTypeGroup() As Integer
        Get
            Return _PaymentTypeGroup
        End Get
        Set(ByVal value As Integer)
            _PaymentTypeGroup = value
        End Set
    End Property

    Public Property PaymentMode() As Integer
        Get
            Return _PaymentMode
        End Get
        Set(ByVal value As Integer)
            _PaymentMode = value
        End Set
    End Property

    Private Sub frmAddPayment_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Loaded

        ' get payment types 
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        Me.Title = "Add Payment in " & CommonDataFunctions.GetCurrencyCodeDesc(GlobalCRM.CurrentCaseID)
        m_strCurrencyChar = CommonDataFunctions.GetCurrencyCodeCharacter(GlobalCRM.CurrentCaseID)
        With Me.drpType
            .DisplayMemberPath = "PaymentType_Name"
            .SelectedValuePath = "PaymentType_Name"
            If Me.PaymentTypeGroup = 1 Then
                .ItemsSource = CType(Accounts.GetPaymentTypes(1), IListSource).GetList
                Me.PaymentMode = 1
            ElseIf Me.PaymentTypeGroup = 2 Then
                .ItemsSource = CType(Accounts.GetPaymentTypes(2), IListSource).GetList
                Me.PaymentMode = -1
            Else
                CommonFuncs.DisplayMessage("No Payment Type Mode set", CommonDialog.CustomDialogIcons.Information, "Payment Type Mode")
            End If
            Me.drpType.SelectedIndex = -1
        End With


        With drpMethod
            .DisplayMemberPath = "PaymentMethod_Name"
            .SelectedValuePath = "PaymentMethod_Name"
            .ItemsSource = CType(Accounts.GetPaymentMethods(), IListSource).GetList
            .SelectedIndex = -1
        End With

        ' get payment methods


        Me.txtAmount.Text = String.Empty
        Me.chkPrintReceipt.IsChecked = False
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim tsk As New TasksEvents
        Dim strMessage As String = String.Empty
        Dim SQL As New StringBuilder
        Dim dtbTable As New DataTable
        Dim LngPaymentID As Long

        Try

            ' validate form
            If Me.drpMethod.SelectedIndex = -1 Then
                CommonFuncs.DisplayMessage("You must select a Payment Method.", CommonDialog.CustomDialogIcons.Information, "Error")
                Exit Sub
            End If

            If Me.drpType.SelectedIndex = -1 Then
                CommonFuncs.DisplayMessage("You must select a Payment Type.", CommonDialog.CustomDialogIcons.Information, "Error")
                Exit Sub
            End If

            If Not IsNumeric(Me.txtAmount.Text) Then
                CommonFuncs.DisplayMessage("Not a valid amount", CommonDialog.CustomDialogIcons.Information, "Error")
                Me.txtAmount.Focus()
                Exit Sub
            End If

            If Me.txtAmount.Text <= 0 Then
                If Me.PaymentTypeGroup = 1 Then
                    CommonFuncs.DisplayMessage("Payment amount must be positive", CommonDialog.CustomDialogIcons.Information, "Error")
                    Exit Sub
                Else
                    CommonFuncs.DisplayMessage("Refund amount must be positive", CommonDialog.CustomDialogIcons.Information, "Error")
                    Exit Sub
                End If
            End If

            ' check for silly amounts
            If Me.txtAmount.Text > 10000 Then
                If MsgBox("Payment amount is over ten thousand, please confirm this is correct.", vbYesNo, "Confirm Amount") <> 6 Then Exit Sub
            End If


            Dim sPaid As Long = CommonDataFunctions.GetPaidAmountFromCaseid(GlobalCRM.CurrentCaseID)
            If sPaid = 0 And GlobalCRM.CurrentCompanyID = 0 Then
                Dim invoicedcharge As Long = Convert.ToDouble(GlobalCRM.InvNet)
                If invoicedcharge <= 0.0 Then
                    CommonFuncs.DisplayMessage("Please invoice the charge before making a payment.", CommonDialog.CustomDialogIcons.Information, "No Quoted Charges")
                    Exit Sub
                Else

                    If txtAmount.Text < 20 * CLng(invoicedcharge) / 100 Then
                        CommonFuncs.DisplayMessage("A minimum deposit of " & 20 * CLng(invoicedcharge) / 100 & " must me made to initiate the case", CommonDialog.CustomDialogIcons.Information, "Error")
                        Exit Sub
                    End If
                End If
            End If

            If Accounts.CreatePayment(GlobalCRM.CurrentCaseID, GlobalCRM.CurrentCandidateID, GlobalCRM.CurrentCompanyID, GlobalCRM.CurrentConName, Me.drpMethod.Text.ToString, Me.drpType.Text.ToString, Me.txtAmount.Text, Me.PaymentMode) = False Then
                CommonFuncs.DisplayMessage("The payment could not be added.", CommonDialog.CustomDialogIcons.Information, "Error")
                Exit Sub
            End If

            If Me.chkPrintReceipt.IsChecked = True Then

                'strMessage = vbCrLf & "Receipt Printed"

                SQL.Append("Select Payment_ID from A_Payments where")
                SQL.Append(" Payment_CaseID = " & GlobalCRM.CurrentCaseID)
                SQL.Append(" And Payment_EnteredBy = '" & GlobalCRM.CurrentConName & "'")
                SQL.Append(" Order By Payment_ID Desc")

                dtbTable = CommonDataFunctions.GetSQLDatatable(SQL.ToString)

                If dtbTable.Rows.Count > 0 Then
                    LngPaymentID = dtbTable.Rows(0).Item("Payment_ID")
                    GlobalCRM.CurrentPaymentId = dtbTable.Rows(0).Item("Payment_ID")
                    Dim frmFinances As New frmFinances

                    If frmFinances.GenerateReceipt = True Then
                    Else

                    End If
                    frmFinances = Nothing
                    'Call MsgBox("Couldn't find payment number " & GlobalCRM.CurrentCaseID & ", receipt not printed", , "Error")
                End If
            End If
            If PaymentTypeGroup = 1 Then
                Call tsk.GblFunc_Add_Event("Payment Added", Me.drpType.SelectedValue & " for " & m_strCurrencyChar & Me.txtAmount.Text & " taken by " & Me.drpMethod.SelectedValue, 1, GlobalCRM.CurrentCaseID, 1)
            Else
                Call tsk.GblFunc_Add_Event("Refund Issued", Me.drpType.SelectedValue & " for " & m_strCurrencyChar & Me.txtAmount.Text & " taken by " & Me.drpMethod.SelectedValue, 1, GlobalCRM.CurrentCaseID, 1)
            End If

            RefreshLists()

            Me.Close()
        Catch ex As System.Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
        Finally
            tsk = Nothing
        End Try

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Public Sub RefreshLists()
        'refresh all three lists and the totals
        Dim dt As DataTable
        Dim frmFinances As New frmFinances

        dt = CommonDataFunctions.GetSQLDatatable(Accounts.QuotedCharges(GlobalCRM.CurrentCaseID))
        DataBinding.BindListView(dt, frmFinances.lvwQuoted)

        'frmFinances.FormatCurrencyCols(frmFinances.lvwQuoted)
        dt = CommonDataFunctions.GetSQLDatatable(Accounts.InvoicedCharges(GlobalCRM.CurrentCaseID))
        DataBinding.BindListView(dt, frmFinances.lvwInvoiced)


        'frmFinances.FormatCurrencyCols(frmFinances.lvwInvoiced)
        dt = CommonDataFunctions.GetSQLDatatable(Accounts.Payments(GlobalCRM.CurrentCaseID))
        DataBinding.BindListView(dt, frmFinances.lvwPayments)
        'frmFinances.FormatCurrencyCols(frmFinances.lvwPayments)

    End Sub

    Private Sub frmAddPayment_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles Me.KeyUp
        If e.Key = Key.Escape Then
            Me.Close()
        End If
    End Sub
End Class
